package com.jagochan.imchat.module.user.dao;

import com.jagochan.imchat.module.common.domain.enums.YesOrNoEnum;
import com.jagochan.imchat.module.user.domain.entity.UserBackpack;
import com.jagochan.imchat.module.user.domain.enums.ItemEnum;
import com.jagochan.imchat.module.user.mapper.UserBackpackMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户背包表 服务实现类
 * </p>
 *
 * @author 张国庆
 * @since 2025-07-07
 */
@Service
public class UserBackpackDao extends ServiceImpl<UserBackpackMapper, UserBackpack> {

    public Integer countByItemId(Long uid, Integer itemId) {
        return lambdaQuery()
                .eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getItemId, itemId)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .count();
    }


    public UserBackpack selectOneByUidAndItemId(Long uid, Long itemId) {
        return lambdaQuery()
                .eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getItemId, itemId)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .orderByAsc(UserBackpack::getId)
                .last("limit 1")
                .one();
    }

    public boolean useModifyNameCard(Long id) {
        return lambdaUpdate()
                .eq(UserBackpack::getId, id)
                .eq(UserBackpack::getItemId, ItemEnum.MODIFY_NAME_CARD.getId())
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .set(UserBackpack::getStatus, YesOrNoEnum.YES.getStatus())
                .update();
    }

    public List<UserBackpack> selectByUidAndItemIds(Long uid, List<Long> itemIdList) {
        return lambdaQuery()
                .eq(UserBackpack::getUid, uid)
                .in(UserBackpack::getItemId, itemIdList)
                .orderByAsc(UserBackpack::getId)
                .list();
    }

    public UserBackpack selectByIdempotent(String idempotentKey) {
        return lambdaQuery()
                .eq(UserBackpack::getIdempotent, idempotentKey)
                .one();
    }
}
